package com.pyue.eam.regi.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.aizuda.limiter.annotation.RateLimit;
import com.aizuda.limiter.strategy.IpKeyGenerateStrategy;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pyue.eam.admin.api.constant.ResponseWord;
import com.pyue.eam.admin.api.entity.regi.CqTaskDayEntity;
import com.pyue.eam.admin.api.except.ApiAsserts;
import com.pyue.eam.common.core.util.R;
import com.pyue.eam.common.log.annotation.SysLog;
import com.pyue.eam.common.security.util.SecurityUtils;
import com.pyue.eam.regi.service.CqTaskDayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

/**
 * 任务日报
 *
 * @date 2023-12-23 08:19:25
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/cqTaskDay")
@Tag(description = "cqTaskDay", name = "任务日报管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class CqTaskDayController {
    private final CqTaskDayService cqTaskDayService;

    /**
     * 分页查询
     *
     * @param page      分页对象
     * @param cqTaskDay 任务日报
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    public R getCqTaskDayPage(@ParameterObject Page page, @ParameterObject CqTaskDayEntity cqTaskDay) {
        ApiAsserts.isTrue(cqTaskDay.getTaskId() != null, "任务id不能为空");
        LambdaQueryWrapper<CqTaskDayEntity> wrapper = Wrappers.lambdaQuery(cqTaskDay).orderByAsc(CqTaskDayEntity::getCreateTime);
        return R.ok(cqTaskDayService.page(page, wrapper));
    }

    @Operation(summary = "数据列表", description = "数据列表")
    @GetMapping("/list")
    @RateLimit(count = 1, interval = "3s", strategy = {IpKeyGenerateStrategy.TYPE}, message = ResponseWord.WAIT_TIME)
    public R getCqTaskDay(@ParameterObject CqTaskDayEntity cqTaskDay) {
        ApiAsserts.isTrue(cqTaskDay.getTaskId() != null, "任务id不能为空");
        LambdaQueryWrapper<CqTaskDayEntity> wrapper = Wrappers.lambdaQuery(cqTaskDay).orderByAsc(CqTaskDayEntity::getCreateTime);
        return R.ok(cqTaskDayService.list(wrapper));
    }

    /**
     * 通过id查询任务日报
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(cqTaskDayService.getById(id));
    }

    /**
     * 新增任务日报
     *
     * @param cqTaskDay 任务日报
     * @return R
     */
    @Operation(summary = "新增任务日报", description = "新增任务日报")
    @SysLog("新增任务日报")
    @PostMapping
    public R save(@RequestBody CqTaskDayEntity cqTaskDay) {
        ApiAsserts.isTrue(cqTaskDay.getTaskId() != null, "任务id不能为空");
        cqTaskDay.setUserAvatar(SecurityUtils.getUserAvatar());
        return R.ok(cqTaskDayService.saveData(cqTaskDay));
    }

    /**
     * 修改任务日报
     *
     * @param cqTaskDay 任务日报
     * @return R
     */
    @Operation(summary = "修改任务日报", description = "修改任务日报")
    @SysLog("修改任务日报")
    @PutMapping
    public R updateById(@RequestBody CqTaskDayEntity cqTaskDay) {
        ApiAsserts.isTrue(cqTaskDay.getId() != null, "参数错误",
                cqTaskDay.getTaskId() != null, "任务id不能为空");
        if (StrUtil.isBlank(cqTaskDay.getUserAvatar())) {
            cqTaskDay.setUserAvatar(SecurityUtils.getUserAvatar());
        }
        return R.ok(cqTaskDayService.updateDayReportById(cqTaskDay));
    }

    /**
     * 通过id删除任务日报
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除任务日报", description = "通过id删除任务日报")
    @SysLog("通过id删除任务日报")
    @DeleteMapping
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(cqTaskDayService.removeBatchByIds(CollUtil.toList(ids)));
    }

}
